package net.entelijan.cobean.examples.showcase.multisel;

import java.util.Collections;
import java.util.List;

import net.entelijan.cobean.core.ColumnDesc;
import net.entelijan.cobean.examples.util.Locale;
import net.entelijan.cobean.examples.util.NestedBean;
import net.entelijan.cobean.examples.util.ExamplesUtil;
import net.entelijan.cobean.util.PropertyComparator;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

@SuppressWarnings("unused")
public class ListContentCreator {

	public ListContentCreator() {
		super();
	}

	public static void main(String[] args) {
		List<ColumnDesc> colDescs = ColumnDesc.createPropertyOnlyColumnDescs(
				"ISO3Country", "ISO3Language", "displayCountry",
				"displayLanguage");
		List<Locale> locales = ExamplesUtil.createLocales();
		//Collections.sort(locales, new PropertyComparator<Locale>("ISO3Country", true));
		createContent(locales, colDescs);

	}

	private static void createContent(List<?> rows,
			List<ColumnDesc> columnDescriptions) {
		int cnt1 = 0;
		for (Object row : rows) {
			StringBuilder sb = new StringBuilder();
			if (cnt1++ > 0) {
				sb.append(",\"");
			} else {
				sb.append("\"");
			}
			int cnt = 0;
			BeanWrapper wrow = new BeanWrapperImpl(row);
			for (ColumnDesc cd : columnDescriptions) {
				if (cnt++ > 0) {
					sb.append(" ");
				}
				sb.append(getValue(wrow, cd));
			}
			sb.append("\"");
			System.out.println(sb.toString());
		}

	}

	private static Object getValue(BeanWrapper wrow, ColumnDesc cd) {
		Object val = wrow.getPropertyValue(cd.getPropertyName());
		if (cd.getConverter() != null) {
			val = cd.getConverter().objectToString(val);
		}
		return val;
	}

}
